# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cmake
pkgbase=mingw-w64-${_realname}-bootstrap
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-bootstrap")
pkgver=4.0.1
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.cmake.org/"
msys2_repository_url="https://gitlab.kitware.com/cmake/cmake"
msys2_references=(
  'archlinux: cmake'
  "cpe: cpe:2.3:a:cmake_project:cmake"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-ninja"
         "${MINGW_PACKAGE_PREFIX}-pkgconf")
conflicts=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-cmake-cmcldeps"
  "${MINGW_PACKAGE_PREFIX}-cmake-docs"
)
provides=("${MINGW_PACKAGE_PREFIX}-cmake=${pkgver}")
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0003-fix-find-python-on-mingw-aarch64.patch"
        "0005-Default-to-ninja-generator.patch")
sha256sums=('d630a7e00e63e520b25259f83d425ef783b4661bdc8f47e21c7f23f3780a21e1'
            '557b5cbc05d4d50b3a67a7892391fcaa5cd95c492cdb4338d86305d1f4a3b88a'
            '426818278090704d2a12f62ef3dfd94c47b11fa2784bb842989b7f6a09ee7aa2')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0003-fix-find-python-on-mingw-aarch64.patch"
  patch -Np1 -i "${srcdir}/0005-Default-to-ninja-generator.patch"

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYSTEM=MINGW MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX="\
    "${srcdir}/${_realname}-${pkgver}"/configure \
    --prefix=${MINGW_PREFIX} \
    --datadir=share/cmake \
    --docdir=share/doc/cmake \
    --mandir=share/man \
    --no-system-libs \
    --no-qt-gui \
    --parallel=${NUMBER_OF_PROCESSORS} \
    -- \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"
}
